

on open fileNameAlias
	-- Read in the contents of the HTML file
	set fileAccesObj to (open for access fileNameAlias)
	set fileContentsText to read fileAccesObj
	close access fileAccesObj
	
	-- open a new ToDo List document to put the anchors in
	tell application "ToDo List"
		-- Create a new document to put the items into
		set docNum to make new document
		
		-- Get the name and use it instead since the number could
		-- change if the user makes or opens another document in ToDo List
		-- while we're running
		--set docName to the name of document docNum
		
		-- Set some of the document settings to values more appropriate
		-- to a list of bookmarks
		set the show calendar of document docNum to false
		set the use dates of document docNum to false
		set the clipboard text only of document docNum to true
	end tell
	
	-- Go through it and look for anchors
	set i to offset of "<A" in fileContentsText
	if i = 0 then set i to the offset of "<a" in fileContentsText
	repeat while i > 0
		-- Find the end of the anchor
		set j to the offset of "</A>" in fileContentsText
		if j = 0 then set j to the offset of "</a>" in fileContentsText
		if j = 0 then exit repeat
		
		-- Remove the anchor from the contents string
		set theAnchor to text from character (i + 3) to character (j - 1) of fileContentsText
		set fileContentsText to text from character (j + 4) to end of fileContentsText
		
		-- Find the URL in the anchor
		set k to the offset of "HREF=" in theAnchor
		set theAnchor to text from character (k + 6) to end of theAnchor
		set m to the offset of "\"" in theAnchor
		set theURL to text from character 1 to character (m - 1) of theAnchor
		
		-- Also find the actual tag, since it's usually more meaningful
		-- We need to go backward from the end of the anchor and find the ">" that
		-- closed the <A
		set x to the count of characters in theAnchor
		repeat while character x of theAnchor is not ">" and x > 0
			set x to x - 1
		end repeat
		if (x > 0) then
			set theURL to StripWhitespace(text from character (x + 1) to end of theAnchor) & " (" & theURL & ")"
		end if
		
		tell application "ToDo List"
			-- Put the item at the end of the list by just picking a very large number
			make new item at 32000 with properties {description:theURL} within document docNum -- docName
		end tell
		
		set i to offset of "<A" in fileContentsText
		if i = 0 then set i to the offset of "<a" in fileContentsText
	end repeat
	
end open


on StripWhitespace(theStr)
	
	-- First, remove any carriage returns within the string
	repeat with x from 1 to (the count of characters in theStr)
		if character x of theStr is return then
			set theStr to (text from character 1 to character (x - 1) of theStr) & space & (text from character (x + 1) to end of theStr)
		end if
	end repeat
	
	-- Get rid of any leading whitespace
	set x to 1
	repeat while character x of theStr is space or character x of theStr is return or character x of theStr is tab
		set x to x + 1
	end repeat
	if x > 1 then set theStr to text from character x to end of theStr
	
	-- Remove any trailing whitespace
	set x to the count of characters in theStr
	repeat while x > 0 and (character x of theStr is space or character x of theStr is return or character x of theStr is tab)
		set x to x - 1
	end repeat
	if (x < the (count of characters in theStr)) then set theStr to text from character 1 to character x of theStr
	
	-- And finally, return the stripped string
	return theStr
	
end StripWhitespace